package com.xidian.zhiancloud.service.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.model.syset.*;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.util.constants.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserService extends ServiceImpl<SysUserMapper, SysUser> {

	@Autowired
    private SysUserMapper usermgMapper;
	
	public Page<SysUser> getUserList(Integer orgid, Integer departid, Integer jobid, Integer roleid, BaseReqQuery reqdata){
		PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
		Page<SysUser> pagedata =usermgMapper.getUserList(orgid,departid,jobid, roleid,reqdata.getSearchKey());
		
		return pagedata;
	}
	public SysUser Login(String username, String password){
		return usermgMapper.login(username, password, SystemConstants.passkey);
	}
	
	public List<OrgLeveList> getOrgLeveList(Long companyId)
	{
		return usermgMapper.getOrgLeveList(companyId);
	}
	
	public List<Organization> getOrgList(Long companyId)
	{
		return usermgMapper.getOrgList(companyId);
	}
	
	public List<RoleList> getRoleList()
	{
		return usermgMapper.getRoleList();
	}
	
	public Page<JobList> getJobList(Long companyId,BaseReqQuery reqdata){
		PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
		Page<JobList> pagedata =usermgMapper.getJobList(companyId,reqdata.getSearchKey());
		return pagedata;
	}
	
	public Page<DepartList> getDepartList(Long companyId,BaseReqQuery reqdata,Integer orgid,Integer jobid){
		PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
		Page<DepartList> pagedata =usermgMapper.getDepartList(companyId,reqdata.getSearchKey(),orgid,jobid);
		
		return pagedata;
	}

	public List<MenuList> getMenuRightByRoleid(Integer roleid)
	{
		return usermgMapper.getMenuRightByRoleid(roleid);
	}
	// 获取登录用户的菜单
	public List<MenuList> getMenuByRoleid(String roleids){
		return usermgMapper.getMenuByRoleid(roleids);
	}
	// 获取所有菜单列表
	public List<MenuList> getMenuByAdmin(Integer defant) {
		return usermgMapper.getMenuByAdmin(defant);
	}
	
	public int insertUser(SysUser data){
		return usermgMapper.insertUser(data,SystemConstants.passkey);
	}

	public List<SysUser> selectUserList() {
		// TODO Auto-generated method stub
		return usermgMapper.selectUserList();
	}

	public int deletByUserId(String id){
		List<String> lists = getList(id);
		return usermgMapper.deletByUserId(lists);
	}

	/**
	 * id放入list
	 * @param id id(多个已逗号分隔)
	 * @return List集合
	 */
	public List<String> getList(String id) {
		List<String> list = new ArrayList<String>();
		String[] str = id.split(",");
		for (int i = 0; i < str.length; i++) {
			list.add(str[i]);
		}
		return list;
	}

	public int updateByOrgLevelId(String id,String customtypename){
		return usermgMapper.updateByOrgLevelId(id,customtypename);
	}

	public int deletByJobId(String id){
		List<String> lists = getList(id);
		return usermgMapper.deletByJobId(lists);
	}

	public int addJobMes(JobList job){
		return usermgMapper.addJobMes(job);
	}

	public int updateJobMes(String id, Integer num, String name,Integer createId){
		return usermgMapper.updateJobMes(id,num,name,createId);
	}

	public int updateUser(String id,SysUser data){
		return usermgMapper.updateUser(id,data,SystemConstants.passkey);
	}

	public Page<Organization> getexportOrgList
			(Long companyId){
		return usermgMapper.getexportOrgList(companyId);
	}
	@Transactional
	public String importUserData
			(Long companyId,List<ImportUser> userList,boolean updateSupport){
		List<SysUser> users  = new ArrayList<>();
		
		if(userList!=null && userList.size()>0) {
			for (int i = 0; i < userList.size(); i++) {
				String oidString = "";
				
				String dept =userList.get(i).getDeptName();
				String leaders  = userList.get(i).getLeader();
				String[] depts = dept.split(";");
				String[] ld = leaders.split(";");
 //               List<Integer> oids= null;
                
				List<Long> deptsId = new ArrayList<>();
				if(depts.length>0) {
					for (int j = 0; j < depts.length; j++) {
						String[] d = depts[j].split("/");
						List<Organization> org  = usermgMapper.getdeptListByName(d);
						if(org!=null && org.size()>0) {
							Long oid = getOrgId(org,d);
//                            List olist =  Arrays.asList(org.get(j+1).getId());
//                            oids.addAll(olist);
							if(oid !=null) {
								if(oidString!="") {oidString= oidString+","+oid;}else {oidString += oid;}
								if(ld!=null && ld.length>0) {
									if(ld[j].equals("是")) {
										deptsId.add(oid);
									}
								}
								
							}
						}
						
						
					}
				}
				String didString = "";
				String jidString = "";
				String job = userList.get(i).getJob();
				String[] jobs = job.split(";");
				if(jobs!=null && jobs.length>0) {
					List<JobList> b  = usermgMapper.getJobListByName(jobs);
                    
					if(b!=null && b.size()>0) {
						for (int j = 0; j < b.size(); j++) {
							if(jidString!="") {
								jidString =jidString+","+b.get(j).getId();
							}else {
								 jidString +=b.get(j).getId();
//                                List<Integer> jid= getByJid(String.valueOf(b.get(j).getId()));
//                                List<DepartList> depart = usermgMapper.getByOrgidAndJobid
//                                    (companyId,oids,jid);
//                                for (int k = 0; k < depart.size(); k++) {
//                                    didString +=depart.get(k).getId();    
//                                }
							}
						}
					}
				}
				
				SysUser  user = new SysUser();
				user.setName(userList.get(i).getName());
				user.setPhone(userList.get(i).getPhone());
				user.setJobids(jidString);
				user.setOrgids(oidString);
				user.setDepartids(didString);
				usermgMapper.insertImportUser(user);
				usermgMapper.updateLeaderOrg(deptsId,user.getId());
			}
		}
		return null;
		//return usermgMapper.importUserData(companyId,userList,updateSupport);
	}
	
	public List<Integer> getByOid(String orgid){
        List<Integer> arr = new ArrayList<>();
        List olist =  Arrays.asList(orgid);
        arr.addAll(olist);
        return arr;
    }

    public List<Integer> getByJid(String jobid){
        List<Integer> arr = new ArrayList<>();
        List jlist =  Arrays.asList(jobid);
        arr.addAll(jlist);
        return arr;
    }
	
	public Long getOrgId(List<Organization> org,String[] d) {
		Long paretId = org.get(0).getId();
		String mydept = d[d.length-1];
		for (int i = 0; i < org.size(); i++) {
			if(paretId.equals(org.get(i).getId()) && org.get(i).getName().equals(mydept)) {
				return org.get(i).getId();
			}
			
			if(!paretId.equals(org.get(i).getId()) && org.get(i).getPid().equals(paretId) ) {
				if(org.get(i).getName().equals(mydept)) {
					return org.get(i).getId();
				}else {
					paretId = org.get(i).getId();
				}
			}
		}
		return null;
	}

	public SysUser getByUserAndSuperAdministrator(Long companyId){
	    return usermgMapper.getByUserAndSuperAdministrator(companyId);
    }
    
    public DepartList getByDepartAndIdAndName(Long companyId,String name){
        return usermgMapper.getByDepartAndIdAndName(companyId,name);
    }

    public JobList getByJobAndIdAndName(Long companyId,String name){
        return usermgMapper.getByJobAndIdAndName(companyId,name);
    }

    public int addDepart(DepartList depart){
        return usermgMapper.addDepart(depart);
    }

    public int updateDepart(String id,DepartList depart){
        return usermgMapper.updateDepart(id,depart);
    }

    public int deleteDepart(String id){
        List<String> lists = getList(id);
        return usermgMapper.deleteDepart(lists);
    }
    
    public List<SysUser> getByUserIdAndRoleIds(List<String> jobAnduser){
	    return usermgMapper.getByUserIdAndRoleIds(jobAnduser);
    }

    public List<String> getByUserIDList(Long companyId,String jobids){
        return usermgMapper.getByUserIDList(companyId,jobids);
    }
    
    public List<SysUser> getUserPhonle(Long companyId,String phone){
        return usermgMapper.getUserPhonle(companyId,phone);	    
    }
    
    public int updateByPassword(String id,String newPassword){
        return usermgMapper.updateByPassword(id,newPassword,SystemConstants.passkey);
    }
    
    
}
